package com.mall.service.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.common.CommonPage;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.OmsOrderReturnApply;
import com.mall.model.domain.OmsUpdateStatusParam;
import com.mall.model.vo.OmsReturnApplyVo;
import com.mall.service.order.service.OmsOrderReturnApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/returnApply")
public class OmsOrderReturnApplyController {

    @Autowired
    OmsOrderReturnApplyService omsOrderReturnApplyService;


    /**
     * 分页查询退货申请
     * @param pageNum
     * @param pageSize
     * @param createTime
     * @param handleMan
     * @param handleTime
     * @param id
     * @param receiverKeyword
     * @param status
     * @return
     */
    @GetMapping("/list")
    public CommonResult<CommonPage> getReturnApply(@RequestParam(defaultValue = "1") Integer pageNum,
                                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                                 String createTime,String handleMan,String handleTime,Long id,String receiverKeyword,Long status){
        Page<OmsOrderReturnApply> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<OmsOrderReturnApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(createTime != null, OmsOrderReturnApply::getCreateTime, createTime);
        queryWrapper.eq(handleMan != null,OmsOrderReturnApply::getHandleMan,handleMan);
        queryWrapper.eq(handleTime != null,OmsOrderReturnApply::getHandleTime,handleTime);
        queryWrapper.eq(id != null,OmsOrderReturnApply::getId,id);
        queryWrapper.like(receiverKeyword != null,OmsOrderReturnApply::getReturnName,receiverKeyword).or().eq(receiverKeyword != null,OmsOrderReturnApply::getReturnPhone,receiverKeyword);
        queryWrapper.eq(status != null,OmsOrderReturnApply::getStatus,status);
        omsOrderReturnApplyService.page(page,queryWrapper);
        CommonPage page1 = CommonPage.getPage(page);
        return CommonResult.success(page1);
    }

    /**
     * 获取退货申请详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public CommonResult<OmsReturnApplyVo> getReturnApplyInfo(@PathVariable Long id){
        OmsReturnApplyVo omsReturnApplyVo = omsOrderReturnApplyService.getByIdWithAddress(id);
        return CommonResult.success(omsReturnApplyVo);
    }

    /**
     * 批量删除退货申请
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public CommonResult del(@RequestParam List<Long> ids){
        omsOrderReturnApplyService.removeByIds(ids);
        return CommonResult.success("删除成功");
    }

    /**
     * 修改退货申请状态
     * @param id
     * @param omsUpdateStatusParam
     * @return
     */
    @PostMapping("/update/status/{id}")
    public CommonResult updateStatus(@PathVariable Long id,@RequestBody OmsUpdateStatusParam omsUpdateStatusParam){
        omsOrderReturnApplyService.updateReturnApplyStatus(id,omsUpdateStatusParam);
        return CommonResult.success("修改成功");
    }



}
